package com.example.yichuangyun.mvvm.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemRiskInfoBinding;
import com.example.yichuangyun.model.company.GsInfoAllModel;

import java.util.List;

/**
 * 工商变更
 */
public class GsBaseInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private final List<GsInfoAllModel> data;

    public GsBaseInfoAdapter(Context context, List<GsInfoAllModel> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemRiskInfoBinding binding = ItemRiskInfoBinding.inflate(LayoutInflater.from(context), parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            GsInfoAllModel model = data.get(position);
            switch (position) {
                case 0: // 基本信息
                    myViewHolder.binding.recyclerView.setAdapter(new CompanyIntroAdapter(context, model.getInfoList()));
                    myViewHolder.binding.ivArrow.setVisibility(View.VISIBLE);
                    myViewHolder.binding.tvNoData.setVisibility(View.GONE);
                    myViewHolder.binding.tvCount.setVisibility(View.GONE);
                    break;
                case 1: // 股东信息
                    myViewHolder.binding.tvCount.setText(context.getString(R.string.tab_count, model.getPartnersAlls().size()));
                    myViewHolder.binding.recyclerView.setAdapter(new GdInfoAdapter(context, model.getPartnersAlls()));
                    myViewHolder.binding.ivArrow.setVisibility(model.getPartnersAlls().size() > 0 ? View.VISIBLE : View.GONE);
                    myViewHolder.binding.tvNoData.setVisibility(model.getPartnersAlls().size() > 0 ? View.GONE : View.VISIBLE);
                    myViewHolder.binding.tvCount.setVisibility(model.getPartnersAlls().size() > 0 ? View.VISIBLE : View.GONE);
                    break;
                case 2: // 主要人员
                    myViewHolder.binding.tvCount.setText(context.getString(R.string.tab_count, model.getEmployeesAlls().size()));
                    myViewHolder.binding.recyclerView.setAdapter(new MainPersonInfoAdapter(context, model.getEmployeesAlls()));
                    myViewHolder.binding.ivArrow.setVisibility(model.getEmployeesAlls().size() > 0 ? View.VISIBLE : View.GONE);
                    myViewHolder.binding.tvNoData.setVisibility(model.getEmployeesAlls().size() > 0 ? View.GONE : View.VISIBLE);
                    myViewHolder.binding.tvCount.setVisibility(model.getEmployeesAlls().size() > 0 ? View.VISIBLE : View.GONE);
                    break;
                case 3: // 工商变更
                    myViewHolder.binding.tvCount.setText(context.getString(R.string.tab_count, model.getCount()));
                    myViewHolder.binding.recyclerView.setAdapter(new GsChangeAdapter(context, model.getRecords()));
                    myViewHolder.binding.ivArrow.setVisibility(model.getRecords().size() > 0 ? View.VISIBLE : View.GONE);
                    myViewHolder.binding.tvNoData.setVisibility(model.getRecords().size() > 0 ? View.GONE : View.VISIBLE);
                    myViewHolder.binding.tvCount.setVisibility(model.getRecords().size() > 0 ? View.VISIBLE : View.GONE);
                    break;
                case 4: // 企业年报
                    myViewHolder.binding.tvCount.setText(context.getString(R.string.tab_count, model.getReportDetails().size()));
                    myViewHolder.binding.recyclerView.setAdapter(new ReportAdapter(context, model.getReportDetails()));
                    myViewHolder.binding.ivArrow.setVisibility(model.getReportDetails().size() > 0 ? View.VISIBLE : View.GONE);
                    myViewHolder.binding.tvNoData.setVisibility(model.getReportDetails().size() > 0 ? View.GONE : View.VISIBLE);
                    myViewHolder.binding.tvCount.setVisibility(model.getReportDetails().size() > 0 ? View.VISIBLE : View.GONE);
                    break;
            }
            myViewHolder.binding.recyclerView.setVisibility(model.isOpen() ? View.VISIBLE : View.GONE);
            myViewHolder.binding.ivArrow.setImageResource(model.isOpen() ? R.mipmap.icon_up : R.mipmap.icon_down);
            myViewHolder.binding.tvTitle.setText(model.getTitle());

            myViewHolder.binding.llTitle.setEnabled(data.size() > 0);
            myViewHolder.binding.llTitle.setOnClickListener(view -> {
                model.setOpen(!model.isOpen());
                notifyItemChanged(holder.getAbsoluteAdapterPosition());
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemRiskInfoBinding binding;

        public MyViewHolder(ItemRiskInfoBinding itemRiskInfoBinding) {
            super(itemRiskInfoBinding.getRoot());
            binding = itemRiskInfoBinding;
        }
    }
}
